/**
 * @program school-bus-cloud
 * @description: CommonResponse
 * @author: mf
 * @create: 2020/10/31 15:51
 */

package com.eye.catcher.param;

import com.alibaba.fastjson.JSONObject;
import com.eye.catcher.constants.SbCode;
import lombok.Data;

/**
 * @author huzhiwei
 * @since 2022-02-08
 */
@Data
public class CommonResponse extends AbstractResponse{



    private static class SingletonClassInstance{
        private static final CommonResponse instance=new CommonResponse();
    }
    private CommonResponse(){}

    public static CommonResponse getInstance(){
        return SingletonClassInstance.instance;
    }

    private Object data;

    public static CommonResponse success(){
        CommonResponse response = CommonResponse.getInstance();
        response.setCode(SbCode.SUCCESS.getCode());
        response.setMsg(SbCode.SUCCESS.getMessage());
        response.setData(null);
        return response;
    }
    public static CommonResponse inconsistentError(String field){
        CommonResponse response = CommonResponse.getInstance();
        response.setCode(SbCode.SYSTEM_ERROR.getCode());
        response.setMsg(field+" 传参不合规，与登陆人不统一");
        response.setData(null);
        return response;
    }
    public static CommonResponse error(){
        CommonResponse response = CommonResponse.getInstance();
        response.setCode(SbCode.SYSTEM_ERROR.getCode());
        response.setMsg(SbCode.SYSTEM_ERROR.getMessage());
        response.setData(null);
        return response;
    }
    public static CommonResponse error(Exception e){
        CommonResponse response = CommonResponse.getInstance();
        response.setCode(SbCode.SYSTEM_ERROR.getCode());
        if(e.getMessage().indexOf("Duplicate")>-1){
            response.setMsg("编号重复");
        }else{
            response.setMsg(SbCode.SYSTEM_ERROR.getMessage());
        }
        response.setData(null);
        return response;
    }
    public static CommonResponse uploadRepeat(JSONObject jsonObject) {
        CommonResponse response = CommonResponse.getInstance();
        response.setCode(SbCode.UPLOAD_FILE_REPEAT.getCode());
        response.setMsg(SbCode.UPLOAD_FILE_REPEAT.getMessage());
        response.setData(jsonObject.toString());
        return response;
    }
    public static CommonResponse uploadException(JSONObject jsonObject) {
        CommonResponse response = CommonResponse.getInstance();
        response.setCode(SbCode.UPLOAD_FILE_NO_EXCEPTION.getCode());
        response.setMsg(SbCode.UPLOAD_FILE_NO_EXCEPTION.getMessage());
        response.setData(jsonObject.toString());
        return response;
    }
}
